/*
 * ContribuyentesImpuestosVerFrame.java
 *
 * Created on 24 de abril de 2008, 06:49 PM
 */
package org.remolques.registros;

import org.mybeans.Sesion;
 


import org.mybeans.RegimenRemolquesBeans;
import org.mybeans.RemolquesBeans;
import org.remolques.busquedas.RemolquesBusquedaConPlacaFrame;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import org.menu.MenuRemolques;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author  avbravo
 */
public class RemolquesImpuestosRegistrosFrame extends javax.swing.JFrame {

    boolean focoRemolque = false;
    boolean focoImpuestos = false;
   
    Sesion sesion;
   RemolquesBeans remolquesBeans;
    RegimenRemolquesBeans regimenRemolquesBeans;
    int fila = -1;
    int Anio;
    int NumeroMes;
    DefaultTableModel modelo;
    String Placa;
    int IdRemolque;

    /** Creates new form ContribuyentesImpuestosVerFrame */
    public RemolquesImpuestosRegistrosFrame() {
        initComponents();
        sesion = new Sesion();


        /*
         * Fondo
         */

         
         
         

        jPanel3.setOpaque(false);
        setContentPane(jPanel3);

        /*
         * fin del fondo
         */
        remolquesBeans = new RemolquesBeans();
        regimenRemolquesBeans = new RegimenRemolquesBeans();

        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        this.Anio = ca.get(java.util.Calendar.YEAR);
        NumeroMes = ca.get(java.util.Calendar.MONTH);

        sesion.setAnio(this.Anio);

        modelo = (DefaultTableModel) jXTable1.getModel();


        TableColumn columnId = jXTable1.getColumnModel().getColumn(0);
        columnId.setPreferredWidth(130);

        TableColumn columnRegimen = jXTable1.getColumnModel().getColumn(1);
        columnRegimen.setPreferredWidth(130);



         


//        popupMenu.add(menuEliminar);

        Limpiar();
   MenuRemolques menuRemolques = new MenuRemolques();
        menuRemolques.VerificarPrivilegios();


        if (menuRemolques.isBjMenuItemRemolquesImpuestosAgregar()) {
            jButtonAgregar.setVisible(true);
        } else {
            jButtonAgregar.setVisible(false);
        }


        if (menuRemolques.isBjMenuItemRemolquesImpuestosEliminar()) {
            jButtonEliminar.setVisible(true);

        } else {
            jButtonEliminar.setVisible(false);
        }
    


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rectanglePainter1 = new org.jdesktop.swingx.painter.RectanglePainter();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldPlaca = new javax.swing.JTextField();
        jTextFieldRemolque = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldIdRegimen = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldRegimen = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldValor = new javax.swing.JTextField();
        jButtonImprimir = new javax.swing.JButton();
        jButtonVistaPrevia = new javax.swing.JButton();
        jButtonAgregar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registros de Impuestos de Remolques");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Placa");

        jLabel2.setText("Remolque");

        jTextFieldPlaca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPlacaActionPerformed(evt);
            }
        });

        jTextFieldRemolque.setEditable(false);

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setMnemonic('b');
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Cerrar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Regimen", "Valor", "Periodo", "Mes", "Inscripcion"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jXTable1.setOpaque(false);
        jScrollPane1.setViewportView(jXTable1);

        jLabel5.setText("Codigo");

        jTextFieldIdRegimen.setEditable(false);

        jLabel6.setText("Regimen");

        jTextFieldRegimen.setEditable(false);

        jLabel7.setText("Valor");

        jTextFieldValor.setEditable(false);
        jTextFieldValor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldValorActionPerformed(evt);
            }
        });

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/print.jpeg"))); // NOI18N
        jButtonImprimir.setToolTipText("Imprimir");
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        jButtonVistaPrevia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/preview.png"))); // NOI18N
        jButtonVistaPrevia.setToolTipText("Vista Previa");
        jButtonVistaPrevia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVistaPreviaActionPerformed(evt);
            }
        });

        jButtonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/impuestos.png"))); // NOI18N
        jButtonAgregar.setToolTipText("Agregar impuestos");
        jButtonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAgregarActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/delete.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButton1.setToolTipText("Regresar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
                        .addGap(75, 75, 75))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jTextFieldPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTextFieldRemolque, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE))
                        .addGap(120, 120, 120))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(56, 56, 56)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldIdRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextFieldPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                            .addComponent(jButtonRegresar))
                        .addGap(6, 6, 6))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jButtonAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldRemolque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldIdRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed

    RemolquesBusquedaConPlacaFrame remolquesBusquedaConPlacaFrame = new RemolquesBusquedaConPlacaFrame();
    remolquesBusquedaConPlacaFrame.setLocationRelativeTo(null);
    remolquesBusquedaConPlacaFrame.setVisible(true);
    focoRemolque = true;
    focoImpuestos = false;
}//GEN-LAST:event_jButtonBuscarActionPerformed

private void jTextFieldValorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldValorActionPerformed
}//GEN-LAST:event_jTextFieldValorActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jTextFieldPlacaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPlacaActionPerformed
    Cargarremolques();


}//GEN-LAST:event_jTextFieldPlacaActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {

        if (focoRemolque) {

            Actualizar();
        }
        else{
            if (focoImpuestos = true){
               CargarImpuestos();
            }
        }

        focoRemolque = false;
        focoImpuestos = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Boton Actualizar Impuestos()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed


    String Placa = this.jTextFieldPlaca.getText();
    if (Placa == null || Placa.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese la placa ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldPlaca.requestFocus();
        return;
    }

    String remolque = this.jTextFieldRemolque.getText();
    if (remolque == null || remolque.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese la placa", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldRemolque.requestFocus();
        return;
    }
    String reportSource;
    reportSource = sesion.getRutaReportes() + "RemolquesImpuestos.jrxml";
    String reportDest;


    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_PLACA", Placa);
        params.put("P_IDREMOLQUE", this.IdRemolque);
        params.put("P_REMOLQUE", remolque);
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperPrintManager.printReport(jasperPrint, true);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

private void jButtonVistaPreviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVistaPreviaActionPerformed


    String Placa = this.jTextFieldPlaca.getText();
    if (Placa == null || Placa.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese la placa ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldPlaca.requestFocus();
        return;
    }

    String remolque = this.jTextFieldRemolque.getText();
    if (remolque == null || remolque.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese la placa", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldRemolque.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "RemolquesImpuestos.jrxml";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_PLACA", Placa);
        params.put("P_IDREMOLQUE", this.IdRemolque);
        params.put("P_REMOLQUE", remolque);
        params.put("P_MUNICIPIO", sesion.getMunicipio());

        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperViewer.viewReport(jasperPrint, false);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonVistaPreviaActionPerformed

private void jButtonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAgregarActionPerformed
    // TODO add your handling code here:
      if (this.IdRemolque == 0) {
        JOptionPane.showMessageDialog(this, "Ingrese la placa del remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        return;
    }
    sesion.setIdRemolque(this.IdRemolque);
    RemolquesImpuestosInscripcionAgregarFrame remolquesImpuestosInscripcionAgregarFrame = new RemolquesImpuestosInscripcionAgregarFrame();
    remolquesImpuestosInscripcionAgregarFrame.setLocationRelativeTo(null);
    remolquesImpuestosInscripcionAgregarFrame.setVisible(true);
    focoRemolque = false;
    focoImpuestos = true;
}//GEN-LAST:event_jButtonAgregarActionPerformed

private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
    // TODO add your handling code here:
    try {

            fila = jXTable1.getSelectedRow();
            if (fila == -1) {
                JOptionPane.showMessageDialog(this, "No se selecciono ninguna fila", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }
            String IdRegimen = String.valueOf(jXTable1.getValueAt(fila, 0));

            if (IdRegimen == null || IdRegimen.equals("")) {
                sesion.MostrarMensajeAdvertencia("Mensaje", "Seleccione un regimen");
                return;
            }
            String Regimen = String.valueOf(jXTable1.getValueAt(fila, 1));
            if (Regimen == null || Regimen.equals("")) {
                sesion.MostrarMensajeAdvertencia("Mensaje", "Seleccione un regimen");
                return;
            }
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            //poner el estatus a inactivo
            int res = JOptionPane.showConfirmDialog(this, "Desea eliminar este regimen " + IdRegimen + "\n Regimen: " + Regimen,
                    "Eliminar Regimen ", JOptionPane.YES_NO_OPTION);
            if (res == JOptionPane.YES_OPTION) {

                String sql = " delete from regimenremolques  where idmunicipio=? and IdRemolque= ? and idregimen=? and activo='si' ";
                PreparedStatement prest = sesion.con.prepareStatement(sql);
                prest.setString(1, sesion.getIdmunicipio());
                prest.setInt(2, this.IdRemolque);
                prest.setString(3, IdRegimen);
                prest.executeUpdate();

                 //-------------------------------------*

            java.util.Calendar ca = java.util.Calendar.getInstance();
            java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());

            int Anio = ca.get(java.util.Calendar.YEAR);

            //pagosmensuales
            sql = " delete from  pagosmensuales "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=? and activo='si' ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, String.valueOf(this.IdRemolque));
            prest.setString(3, "remolques");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();


            //pagosanuales
            sql = " delete from  pagosanuales "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=? and activo='si' ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, String.valueOf(this.IdRemolque));
            prest.setString(3, "remolques");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();


             //morosidadmensual
            sql = " delete from  morosidadmensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, String.valueOf(this.IdRemolque));
            prest.setString(3, "remolques");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();

              //recargomensual
            sql = " delete from  recargomensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, String.valueOf(this.IdRemolque));
            prest.setString(3, "remolques");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();


             //cierremorosidadmensual
            sql = " delete from  cierremorosidadmensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, String.valueOf(this.IdRemolque));
            prest.setString(3, "remolques");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();

//cierremorosidadmensualanual
            sql = " delete from  cierremorosidadmensualanual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, String.valueOf(this.IdRemolque));
            prest.setString(3, "remolques");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();

             //cierrerecargomensual
            sql = " delete from  cierrerecargomensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, String.valueOf(this.IdRemolque));
            prest.setString(3, "remolques");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();

                sesion.con.close();
                JOptionPane.showMessageDialog(this, "Regimen eliminando exitosamente", "Eliminado", JOptionPane.INFORMATION_MESSAGE);
                CargarImpuestos();
            } else {
                JOptionPane.showMessageDialog(this, "No se elimino el regimen.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "jMenuItemPagarActionPerformed()\n " + ex.getMessage().toString());
        }
}//GEN-LAST:event_jButtonEliminarActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jButton1ActionPerformed

   private void Cargarremolques() {

        try {
            Limpiar();
            String Placa = this.jTextFieldPlaca.getText();
            if (Placa == null || Placa.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese la placa del remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            remolquesBeans.setPlaca(Placa);

            if (remolquesBeans.ConsultarPorPlaca() == true) {

                if (remolquesBeans.getPlaca() == null || remolquesBeans.getPlaca().equals("")) {
                    JOptionPane.showMessageDialog(this, "Este remolque no tiene asiganda una placa...", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }


                remolquesBeans.setTipoId(remolquesBeans.getTipoId().toLowerCase());

                Actualizar();

            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }


    }

    private void Actualizar() {
        try {

            jTextFieldPlaca.setText(remolquesBeans.getPlaca());
            this.Placa = remolquesBeans.getPlaca();
            if (remolquesBeans.getIdRemolque() == -1) {
                this.IdRemolque = 0;
                 JOptionPane.showMessageDialog(this, "Este remolque no tiene un codigo asignado", "Advertencia", JOptionPane.WARNING_MESSAGE);
            } else {
                this.IdRemolque = remolquesBeans.getIdRemolque();
            }

            String dueno = "";
            if (remolquesBeans.getDueno() == null) {
                dueno = "";
            } else {
                dueno = " Dueño " + remolquesBeans.getDueno();
            }
            jTextFieldRemolque.setText(remolquesBeans.getMarca() + " Modelo: " + remolquesBeans.getModelo() + dueno);

            String TipoId = remolquesBeans.getTipoId();
            String idOld = remolquesBeans.getId();
            TipoId = TipoId.toLowerCase();
            if (TipoId.equals("contribuyentes")) {
            } else {
                if (TipoId.equals("comercios")) {
                }
            }



 CargarImpuestos();
            repaint();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RemolquesImpuestosRegistrosFrame().setVisible(true);
            }
        });
    }

    
    public void CargarImpuestos() {
        try {
            Limpiar();
            String cedula = jTextFieldPlaca.getText();
            String sql = "select regimenremolques.*, regimen.regimen from  regimenremolques, regimen where regimenremolques.idmunicipio=? and regimenremolques.IdRemolque =? and regimenremolques.activo='si' and regimenremolques.idregimen = regimen.idregimen and regimen.idmunicipio=?";
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            psquery.setObject(1, sesion.getIdmunicipio());
            psquery.setObject(2, this.IdRemolque);
            psquery.setObject(3, sesion.getIdmunicipio());
            ResultSet rs = psquery.executeQuery();

            // modelo = new DefaultTableModel();
            // Creamos las columnas.
            // modelo.addColumn("codigo");
            // modelo.addColumn("regimen");
            // modelo.addColumn("valor");
            // modelo.addColumn("periodo");
            //modelo.addColumn("mes");
            //modelo.addColumn("Inscripcion");

// Bucle para cada resultado en la consulta
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[6]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idregimen");
                fila[1] = rs.getString("regimen");
                fila[2] = rs.getString("valor");
                fila[3] = rs.getString("periodopago");
                fila[4] = rs.getString("mespago");
                java.util.Calendar ca = java.util.Calendar.getInstance();
                java.sql.Date mydate = rs.getDate("fechainscripcion");
                //fila[5] = rs.getDate("fechainscripcion");
                Format formatter;
                formatter = new SimpleDateFormat("dd/MM/yyyy");
                String s = formatter.format(mydate);
                fila[5] = s;
                modelo.addRow(fila);

            }
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);

            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isRightMouseButton(e)) {

                    }
                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                        String idRegimen = String.valueOf(jXTable1.getValueAt(fila, 0));
                        String regimen = String.valueOf(jXTable1.getValueAt(fila, 1));
                        String Svalor = String.valueOf(jXTable1.getValueAt(fila, 2));
                        Double valor = Double.parseDouble(Svalor);
                        String periodoPago = String.valueOf(jXTable1.getValueAt(fila, 3));
                        String mesPago = String.valueOf(jXTable1.getValueAt(fila, 4));
                        regimenRemolquesBeans.setIdRegimen(idRegimen);
                        regimenRemolquesBeans.setRegimen(regimen);
                        regimenRemolquesBeans.setValor(valor);
                        regimenRemolquesBeans.setPeriodoPago(periodoPago);
                        regimenRemolquesBeans.setMesPago(mesPago);
                        jTextFieldIdRegimen.setText(idRegimen);
                        jTextFieldRegimen.setText(regimen);
                        jTextFieldValor.setText(String.valueOf(valor));

                    }
                }
            });
            TableColumn column = jXTable1.getColumnModel().getColumn(1);
            column.setPreferredWidth(250);
            sesion.con.close();
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error", "CargarImpuestos()" + ex.getMessage().toString());
        }

    }

    private void Limpiar() {
        jTextFieldIdRegimen.setText("");
        jTextFieldRegimen.setText("");
        jTextFieldValor.setText("");
        LimpiarTable();
    }

    private void LimpiarTable() {
        try {
            //limpiar  el jXTable

            boolean control = false;
            while (control == false) {
                int n = jXTable1.getRowCount();
                //System.out.println("n" + n);
                if (n <= 0) {
                    control = true;
                } else {
                    modelo.removeRow(0);
                }

            }//while
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error", "LimpiarTable()" + ex.getMessage().toString());
        }
    }

  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonAgregar;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVistaPrevia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldIdRegimen;
    private javax.swing.JTextField jTextFieldPlaca;
    private javax.swing.JTextField jTextFieldRegimen;
    private javax.swing.JTextField jTextFieldRemolque;
    private javax.swing.JTextField jTextFieldValor;
    private org.jdesktop.swingx.JXTable jXTable1;
    private org.jdesktop.swingx.painter.RectanglePainter rectanglePainter1;
    // End of variables declaration//GEN-END:variables
}
